package com.deyuanyun.pic.risk.service.impl;

import com.deyuanyun.pic.common.util.ObjectUtil;
import com.deyuanyun.pic.risk.controller.dto.HcaInfoQuery;
import com.deyuanyun.pic.risk.controller.dto.HcaStatisticsQuery;
import com.deyuanyun.pic.risk.controller.vo.HcaStatisticsObjVO;
import com.deyuanyun.pic.risk.controller.vo.HcaStatisticsVO;
import com.deyuanyun.pic.risk.domain.HcaInfo;
import com.deyuanyun.pic.risk.enumeration.HcaGrade;
import com.deyuanyun.pic.risk.mapper.HcaInfoDao;
import com.deyuanyun.pic.risk.service.HcaService;
import com.deyuanyun.pic.settings.domain.base.Code;
import com.deyuanyun.pic.settings.support.biz.CodeUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class HcaServiceImpl implements HcaService {

	@Resource
	HcaInfoDao hcaInfoDao;

	@Override
	public Page<HcaInfo> queryHca(HcaInfoQuery hcaInfoQuery) {
		return hcaInfoDao.queryHca(hcaInfoQuery);
	}

	@Override
	public HcaStatisticsVO statistics(HcaStatisticsQuery hcaStatisticsQuery) {
		HcaStatisticsVO hcaStatisticsVO = new HcaStatisticsVO();
		List<HashMap> list  = hcaInfoDao.countHacGrade(hcaStatisticsQuery);
		BigDecimal gradeOneNum = BigDecimal.ZERO;
		BigDecimal gradeTwoNum = BigDecimal.ZERO;
		BigDecimal gradeThreeNum = BigDecimal.ZERO;
		for (HashMap map : list){
			Code code = CodeUtil.getCodeById(map.get("hcaGrade".toUpperCase()).toString());
			if (HcaGrade.HAC_GRADE_I.toString().equals(code.getMinorCode())) {
				gradeOneNum = (BigDecimal) map.get("countNum".toUpperCase());
			}
			if (HcaGrade.HAC_GRADE_II.toString().equals(code.getMinorCode())) {
				gradeTwoNum = (BigDecimal) map.get("countNum".toUpperCase());
			}
			if (HcaGrade.HAC_GRADE_III.toString().equals(code.getMinorCode())) {
				gradeThreeNum = (BigDecimal) map.get("countNum".toUpperCase());
			}
		}
		BigDecimal[] countHcaArray = {gradeOneNum, gradeTwoNum, gradeThreeNum};
		hcaStatisticsVO.setCountHacGradeList(countHcaArray);

		// 拆分高后果区等级数据
		List<HcaStatisticsObjVO> hacGradeList = hcaInfoDao.queryHacGrade(hcaStatisticsQuery);
		List<Object[]> tempArray = new ArrayList<Object[]>();
		if (list == null && list.size() <= 0) {
			return null;
		}
		for (HcaStatisticsObjVO hcaStatisticsObjVO : hacGradeList){
			Object[] array = new Object[3];
			array[0] = hcaStatisticsObjVO.getMileage();
			array[1] = hcaStatisticsObjVO.getEndMileage();
			array[2] = hcaStatisticsObjVO.getSvalue();
			tempArray.add(array);
		}
		hcaStatisticsVO.setQueryHacGradeList(tempArray);
		return hcaStatisticsVO;
	}

}
